import {
  Component,
  OnInit,
  ElementRef,
  Renderer,
  Input,
  Output,
  EventEmitter,
  ViewChildren,
  QueryList,
} from '@angular/core';
import {FilterItemComponent} from "@shared/components/filter/filter-item/filter-item.component";
import {ViewEncapsulation} from '@angular/core';
import {EventsService} from "@core/events/events.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {GoodsCategory} from "@interface/index";




@Component({
  selector: 'seed-filter',
  templateUrl: './filter.component.html',
  styleUrls: ['./filter.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class FilterComponent implements OnInit {
  public store: GoodsCategory;
  @Input() data: Array<GoodsCategory>;
  @Output()
  onConfirm: EventEmitter<Object> = new EventEmitter();
  @ViewChildren(FilterItemComponent)
  filterItems: QueryList<FilterItemComponent>;

  constructor(public ele: ElementRef,
              public renderer: Renderer,
              public events: EventsService,
              public tools: ToolsService) {
  }

  ngOnInit() {
  }


  onStateSelected(event) {
    this.slidePrev();
  }

  slideNext() {
    const node = this.ele.nativeElement.querySelector('.seed-filter-box');
    this.renderer.setElementStyle(node, 'transform', 'translateX(-50%)');
  }

  slidePrev() {
    const node = this.ele.nativeElement.querySelector('.seed-filter-box');
    this.renderer.setElementStyle(node, 'transform', 'translateX(0%)');
  }

  confirm() {
    this.filterItems.toArray().forEach((item, i) => {
      if (item.store) {
        this.store = item.store;
      };
    });
    if (this.data) {
      this.onConfirm.emit(this.store);
    };
    this.events.publish('modal:close');
  }

  build(data) {
    let obj = {},
      type = ['attrCode', 'brandCode', 'cateCode'];

    const maps = data.map((item, i) => {
      if (typeof item[type[0]] !== 'undefined') {
        obj[type[0]] = item[type[0]];
      }
      /*      if(typeof item[type[1]] != 'undefined'){
       obj[type[1]] = item[type[1]]
       }
       if(typeof item[type[2]] != 'undefined'){
       obj[type[2]] = item[type[2]]
       }*/
      return obj;
    });
  }

  reset() {
    this.store = null;
    this.filterItems.toArray().forEach((item, i) => {
      item.reset();
    });
  }

}
